package com.ruoyi.reportsnhnt.service.impl;

import cn.hutool.core.bean.BeanUtil;
import com.ruoyi.common.utils.StringUtils;
import com.ruoyi.common.core.page.TableDataInfo;
import com.ruoyi.common.core.domain.PageQuery;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.core.toolkit.Wrappers;
import com.ruoyi.syrw.annotation.FIllBasicInfo;
import com.ruoyi.reportsnhnt.domain.ReportSnhntNjsjBaDetail;
import com.ruoyi.reportsnhnt.mapper.ReportSnhntNjsjBaDetailMapper;
import lombok.RequiredArgsConstructor;
import org.springframework.stereotype.Service;
import com.ruoyi.reportsnhnt.domain.bo.ReportSnhntNjsjBsInfoBo;
import com.ruoyi.reportsnhnt.domain.vo.ReportSnhntNjsjBsInfoVo;
import com.ruoyi.reportsnhnt.domain.ReportSnhntNjsjBsInfo;
import com.ruoyi.reportsnhnt.mapper.ReportSnhntNjsjBsInfoMapper;
import com.ruoyi.reportsnhnt.service.IReportSnhntNjsjBsInfoService;

import java.util.ArrayList;
import java.util.List;
import java.util.Map;
import java.util.Collection;

/**
 * 混凝土凝结时间试验检测记录Service业务层处理
 *
 * @author zxs
 * @date 2024-06-21
 */
@RequiredArgsConstructor
@Service
public class ReportSnhntNjsjBsInfoServiceImpl implements IReportSnhntNjsjBsInfoService {

    private final ReportSnhntNjsjBsInfoMapper baseMapper;
    private final ReportSnhntNjsjBaDetailMapper detailMapper;

    /**
     * 查询混凝土凝结时间试验检测记录
     */
    @Override
    @FIllBasicInfo
    public ReportSnhntNjsjBsInfoVo queryById(Long excelId) {
        ReportSnhntNjsjBsInfoVo vo = baseMapper.selectVoById(excelId);
        List<ReportSnhntNjsjBaDetail> detailList = detailMapper.selectListByExcelId(excelId);
        List<ReportSnhntNjsjBaDetail> returnList = new ArrayList<>();
        // 必须10条
        for (int i = 1; i <= 10; i++) {
            ReportSnhntNjsjBaDetail detail = new ReportSnhntNjsjBaDetail();
            if (detailList != null && detailList.size() >= i) {
                detail = detailList.get(i - 1);
            }
//            detail.setIndex(i);
            returnList.add(detail);
        }
        vo.setDetailList(returnList);
        return vo;
    }

    /**
     * 查询混凝土凝结时间试验检测记录列表
     */
    @Override
    public TableDataInfo<ReportSnhntNjsjBsInfoVo> queryPageList(ReportSnhntNjsjBsInfoBo bo, PageQuery pageQuery) {
        LambdaQueryWrapper<ReportSnhntNjsjBsInfo> lqw = buildQueryWrapper(bo);
        Page<ReportSnhntNjsjBsInfoVo> result = baseMapper.selectVoPage(pageQuery.build(), lqw);
        return TableDataInfo.build(result);
    }

    /**
     * 查询混凝土凝结时间试验检测记录列表
     */
    @Override
    public List<ReportSnhntNjsjBsInfoVo> queryList(ReportSnhntNjsjBsInfoBo bo) {
        LambdaQueryWrapper<ReportSnhntNjsjBsInfo> lqw = buildQueryWrapper(bo);
        return baseMapper.selectVoList(lqw);
    }

    private LambdaQueryWrapper<ReportSnhntNjsjBsInfo> buildQueryWrapper(ReportSnhntNjsjBsInfoBo bo) {
        Map<String, Object> params = bo.getParams();
        LambdaQueryWrapper<ReportSnhntNjsjBsInfo> lqw = Wrappers.lambdaQuery();
        lqw.eq(bo.getPage() != null, ReportSnhntNjsjBsInfo::getPage, bo.getPage());
        lqw.eq(bo.getTotalPage() != null, ReportSnhntNjsjBsInfo::getTotalPage, bo.getTotalPage());
        lqw.eq(StringUtils.isNotBlank(bo.getJiancdwmc()), ReportSnhntNjsjBsInfo::getJiancdwmc, bo.getJiancdwmc());
        lqw.eq(StringUtils.isNotBlank(bo.getJilbh()), ReportSnhntNjsjBsInfo::getJilbh, bo.getJilbh());
        lqw.eq(StringUtils.isNotBlank(bo.getGongcmc()), ReportSnhntNjsjBsInfo::getGongcmc, bo.getGongcmc());
        lqw.eq(StringUtils.isNotBlank(bo.getGongcbwyt()), ReportSnhntNjsjBsInfo::getGongcbwyt, bo.getGongcbwyt());
        lqw.eq(StringUtils.isNotBlank(bo.getYangpxx()), ReportSnhntNjsjBsInfo::getYangpxx, bo.getYangpxx());
        lqw.eq(StringUtils.isNotBlank(bo.getYangpmc()), ReportSnhntNjsjBsInfo::getYangpmc, bo.getYangpmc());
        lqw.eq(StringUtils.isNotBlank(bo.getYangpbh()), ReportSnhntNjsjBsInfo::getYangpbh, bo.getYangpbh());
        lqw.eq(StringUtils.isNotBlank(bo.getYangpsljzt()), ReportSnhntNjsjBsInfo::getYangpsljzt, bo.getYangpsljzt());
        lqw.eq(bo.getYangplysj() != null, ReportSnhntNjsjBsInfo::getYangplysj, bo.getYangplysj());
        lqw.eq(bo.getShiyjcrq() != null, ReportSnhntNjsjBsInfo::getShiyjcrq, bo.getShiyjcrq());
        lqw.eq(StringUtils.isNotBlank(bo.getShiytjwd()), ReportSnhntNjsjBsInfo::getShiytjwd, bo.getShiytjwd());
        lqw.eq(StringUtils.isNotBlank(bo.getShiytjsd()), ReportSnhntNjsjBsInfo::getShiytjsd, bo.getShiytjsd());
        lqw.eq(StringUtils.isNotBlank(bo.getShiyyj()), ReportSnhntNjsjBsInfo::getShiyyj, bo.getShiyyj());
        lqw.eq(StringUtils.isNotBlank(bo.getPandyj()), ReportSnhntNjsjBsInfo::getPandyj, bo.getPandyj());
        lqw.eq(StringUtils.isNotBlank(bo.getZhuyyqsbmcjbh()), ReportSnhntNjsjBsInfo::getZhuyyqsbmcjbh, bo.getZhuyyqsbmcjbh());
        lqw.eq(StringUtils.isNotBlank(bo.getShebIds()), ReportSnhntNjsjBsInfo::getShebIds, bo.getShebIds());
        lqw.eq(StringUtils.isNotBlank(bo.getHunntzl()), ReportSnhntNjsjBsInfo::getHunntzl, bo.getHunntzl());
        lqw.eq(StringUtils.isNotBlank(bo.getJiaobfs()), ReportSnhntNjsjBsInfo::getJiaobfs, bo.getJiaobfs());
        lqw.eq(bo.getJiassj1() != null, ReportSnhntNjsjBsInfo::getJiassj1, bo.getJiassj1());
        lqw.eq(bo.getJiassj2() != null, ReportSnhntNjsjBsInfo::getJiassj2, bo.getJiassj2());
        lqw.eq(bo.getJiassj3() != null, ReportSnhntNjsjBsInfo::getJiassj3, bo.getJiassj3());
        lqw.eq(bo.getChunsj1() != null, ReportSnhntNjsjBsInfo::getChunsj1, bo.getChunsj1());
        lqw.eq(bo.getChunsj2() != null, ReportSnhntNjsjBsInfo::getChunsj2, bo.getChunsj2());
        lqw.eq(bo.getChunsjpjz() != null, ReportSnhntNjsjBsInfo::getChunsjpjz, bo.getChunsjpjz());
        lqw.eq(bo.getChunsj3() != null, ReportSnhntNjsjBsInfo::getChunsj3, bo.getChunsj3());
        lqw.eq(bo.getZhongnsj1() != null, ReportSnhntNjsjBsInfo::getZhongnsj1, bo.getZhongnsj1());
        lqw.eq(bo.getZhongnsj2() != null, ReportSnhntNjsjBsInfo::getZhongnsj2, bo.getZhongnsj2());
        lqw.eq(bo.getZhongnsj3() != null, ReportSnhntNjsjBsInfo::getZhongnsj3, bo.getZhongnsj3());
        lqw.eq(bo.getChunsj() != null, ReportSnhntNjsjBsInfo::getChunsj, bo.getChunsj());
        lqw.eq(bo.getZhongnsj() != null, ReportSnhntNjsjBsInfo::getZhongnsj, bo.getZhongnsj());
        lqw.eq(StringUtils.isNotBlank(bo.getFujsm()), ReportSnhntNjsjBsInfo::getFujsm, bo.getFujsm());
        lqw.eq(StringUtils.isNotBlank(bo.getJianc()), ReportSnhntNjsjBsInfo::getJianc, bo.getJianc());
        lqw.eq(StringUtils.isNotBlank(bo.getJil()), ReportSnhntNjsjBsInfo::getJil, bo.getJil());
        lqw.eq(StringUtils.isNotBlank(bo.getFuh()), ReportSnhntNjsjBsInfo::getFuh, bo.getFuh());
        lqw.eq(bo.getRiq() != null, ReportSnhntNjsjBsInfo::getRiq, bo.getRiq());
        return lqw;
    }

    /**
     * 新增混凝土凝结时间试验检测记录
     */
    @Override
    public Boolean insertByBo(ReportSnhntNjsjBsInfoBo bo) {
        ReportSnhntNjsjBsInfo add = BeanUtil.toBean(bo, ReportSnhntNjsjBsInfo.class);
        validEntityBeforeSave(add);
        boolean flag = baseMapper.insert(add) > 0;
        if (flag) {
            bo.setExcelId(add.getExcelId());
        }
        return flag;
    }

    /**
     * 修改混凝土凝结时间试验检测记录
     */
    @Override
    public Boolean updateByBo(ReportSnhntNjsjBsInfoBo bo) {
        ReportSnhntNjsjBsInfo update = BeanUtil.toBean(bo, ReportSnhntNjsjBsInfo.class);
        validEntityBeforeSave(update);
        //更新子表
        List<ReportSnhntNjsjBaDetail> detailList = bo.getDetailList();
        if (detailList != null) {
            detailList.forEach(vo -> vo.setExcelId(bo.getExcelId()));
            detailMapper.deleteByExcelId(bo.getExcelId());
        }
        detailMapper.insertBatch(detailList);
        return baseMapper.updateById(update) > 0;
    }

    /**
     * 保存前的数据校验
     */
    private void validEntityBeforeSave(ReportSnhntNjsjBsInfo entity) {
        //TODO 做一些数据校验,如唯一约束
    }

    /**
     * 批量删除混凝土凝结时间试验检测记录
     */
    @Override
    public Boolean deleteWithValidByIds(Collection<Long> ids, Boolean isValid) {
        if (isValid) {
            //TODO 做一些业务上的校验,判断是否需要校验
        }
        return baseMapper.deleteBatchIds(ids) > 0;
    }
}
